﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sdk
{
    public class FieldInfo
    {
        public string FieldName { get; set; } = "";

        public string FieldTypeDb { get; set; } = "";

        public FieldTypeEnum FieldType {  get; set; } = FieldTypeEnum.String;

        public string FieldMark { get; set; } = "";

        public bool IsPrimaryKey { get; set; } = false;

        public bool IsIndex { get; set; } = false ;

        public bool IsNullable { get; set; } = true;

        public bool IsAutoGen { get; set; } = false;

        public string Default { get; set; } = "";

        public string FieldComment { get; set; } = "";


        /// <summary>
        /// 解析其他信息
        /// </summary>
        public void Parse()
        {
            //确定字段类型
            foreach (var map in Mappings.FieldTypeMapDict)
            {
                var mapName = map.Key;
                var mapType = map.Value;
                if (FieldTypeDb.Contains(mapName, StringComparison.OrdinalIgnoreCase))
                {
                    FieldType = mapType;
                    break;
                }
            }

            //是否主键
            IsPrimaryKey = IsContainsKeyWord(FieldMark, ["PK", "PRIMARY KEY"]);

            //是否索引
            IsIndex = IsContainsKeyWord(FieldMark, ["IDX", "INDEX", "FK"]);

            //是否可空
            IsNullable = !IsContainsKeyWord(FieldMark, ["NOT NULL"]);

            //是否自增
            IsAutoGen = IsContainsKeyWord(FieldMark, ["AI", "AUTO_INCREMENT", "IDENTITY"]);

            //默认值
            var marks = FieldMark.Split(";", StringSplitOptions.RemoveEmptyEntries);
            string[] keyWords = ["df", "default"];
            var defaultMark = IsContainsKeyWord(marks, keyWords);
            if (!string.IsNullOrWhiteSpace(defaultMark))
            {
                foreach (var keyWord in keyWords)
                {
                    defaultMark = defaultMark.Replace(keyWord, string.Empty);
                }
            }
            Default = defaultMark.Trim();

            FieldComment = FieldComment
                .Replace("\r\n", " ")
                .Replace("\n", " ");
        }


        private bool IsContainsKeyWord(string text, string[] keyWords)
        {
            foreach (var keyWord in keyWords)
            {
                if (text.Contains(keyWord, StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
            }
            return false;
        }

        private string IsContainsKeyWord(string[] marks, string[] keyWords)
        {
            foreach (var keyWord in keyWords)
            {
                foreach (var mark in marks)
                {
                    if (mark.ToLower().Contains(keyWord.ToLower()))
                    {
                        return mark;
                    }
                }
            }
            return "";
        }
    }
}
